package utils

/*
**

	Ubuntu 16.04 修改IP地址
	需要root权限
*/
func ConfigIPAddress_Ubuntu(interace_id, ip, mask, gateway, dns string) error {
	var sb BytesBuilder
	sb.Appendf("auto %s\n", interace_id)
	sb.Appendf("\n")
	sb.Appendf("iface %s inet static\n", interace_id)
	sb.Appendf("address %s\n", ip)
	sb.Appendf("netmask %s\n", mask)
	sb.Appendf("gateway %s\n", gateway)
	sb.Appendf("dns-nameservers %s\n", dns)
	err := RewriteFile("/etc/network/interfaces", sb.Bytes())
	if err != nil {
		return nil
	}

	return ExecShell("/etc/init.d/networking restart")
}

func ReadConfigIPAddress_Ubuntu(interace_id string) (ip, mask, gateway, dns string, err error) {
	data, e1 := ReadFile("/etc/network/interfaces")
	if e1 != nil {
		err = e1
		return
	}
	str := string(data)
	ip = Trim(GetBetween(str, "address", "\n", 0))
	mask = Trim(GetBetween(str, "netmask", "\n", 0))
	gateway = Trim(GetBetween(str, "gateway", "\n", 0))
	dns = Trim(GetBetween(str, "dns-nameservers", "\n", 0))
	return
}
